<%--
  Created by IntelliJ IDEA.
  User: 86187
  Date: 2021/10/21
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="resource/css/bootstrap.css">
    <script src="resource/js/jquery-3.2.1.js"></script>
    <script src="resource/js/bootstrap.js"></script>
    <script src="resource/My97DatePicker/WdatePicker.js"></script>
    <title>列表</title>
<script>
    function getyid(cname){
        $.post(
            "/getyid",
            {cname:cname},
            function (obj){
                if(obj){
                    $("#yId").html(obj.yid);
                    $("[name='yid']").val(obj.yid);
                }
            }
        )
    }
    function getyid2(cname){
        $.post(
            "/getyid",
            {cname:cname},
            function (obj){
                if(obj){
                    $("#objId").html(obj.yid);
                    $("[name='oid']").val(obj.yid);
                }
            }
        )
    }

    function ye() {
        var debit = $("[name='debit']").val();
        var reg_debit = /^[1-9]\d*(\.\d)?$/;
        if (!reg_debit.test(debit)) {
            $("#debit1").text("借方数值为整数、1位小数、2位小数");
        } else {
            $("#debit1").text("");
        }
        var credit = $("[name='credit']").val();
        if (!reg_debit.test(credit)) {
            $("#credit1").text("贷方数值为整数、1位小数、2位小数");
        } else {
            $("#credit1").text("");
        }

        $("[name='balance']").val(debit-credit);
    }

    function tj(){
        ye();
        var str = "";
        $("span").each(function () {
            str += $(this).text();
        })
        if(str==""){
            $.post(
                "/add",
                $("form").serialize(),
                function (obj){
                    if(obj){
                        alert("添加成功");
                        location.href = "index?url=list";
                    }else{
                        alert("添加失败");
                    }
                }
            )
        }

    }
</script>
</head>
<body>
<%--<form:form modelAttribute="company" method="post" action="/add">
    财务日期：<input type="date" name="cwDate"><br>
    源账号名称：<input type="text" name="cname" onblur="getyid(this.value)">
    源账号:<div id="yId">
    </div><br>
    目标账号名称:<input type="text" name="oname">
    目标账号:<div id="objId">
    </div><br>
    科目编号:<input type="text" name="courseId">
    科目:<input type="text" name="courseName"><br>
    借方:<form:input path="debit"></form:input>
    <form:errors path="debit" cssClass="text-danger"></form:errors>
    贷方:<form:input path="credit"></form:input>
    <form:errors path="credit" cssClass="text-danger"></form:errors>
    余额:<input type="text" name="balance"><br>
    摘要:<input type="text" name="digest"><br>
    <input type="submit" value="提交">
</form:form>--%>
<form>
财务日期：<input type="date" name="cwDate"><br>
源账号名称：<input type="text" name="cname" onblur="getyid(this.value)"><br>
    源账号:<div id="yId">
        </div><br>
    目标账号名称:<input type="text" name="oname" onblur="getyid2(this.value)"><br>
    目标账号:<div id="objId">

        </div><br>
    科目编号:<input type="text" name="courseId">
    科目:<input type="text" name="courseName"><br>
    借方:<input type="text" name="debit" onblur="ye()">
        <span id="debit1" style="color: red"></span>
    贷方:<input type="text" name="credit" onblur="ye()">
    <span id="credit1" style="color: red"></span>
    余额:<input type="text" name="balance"><br>
    摘要:<input type="text" name="digest"><br>
    <input type="text" name="yid">
    <input type="text" name="oid">
    <input type="button" onclick="tj()" value="提交">
</form>

</body>
</html>